<template>
  <el-row :gutter="20">
    <el-col :span="15" :xs="24">
      <ArticleCategoryTree @row-click="handleArticleCategoryTreeRowClick" />
    </el-col>
    <el-col :span="9" :xs="24">
      <div class="head-container">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入文章标题"
          clearable
          @keyup.enter="handleQuery"
          class="mb-20px"
        >
          <template #prefix>
            <Icon icon="ep:search" />
          </template>
        </el-input>
      </div>
      <!-- 列表 -->
      <div class="head-container">
        <el-table
          v-loading="loading"
          :data="list"
          :stripe="true"
          :show-overflow-tooltip="true"
          @row-click="handleRowClick"
          highlight-current-row
        >
          <el-table-column label="文章状态" align="center" prop="status">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="文章标题" align="center" prop="title" />
        </el-table>
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
          layout="total, prev, next"
        />
      </div>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import { ArticleDetailApi, ArticleDetailVO } from '@/api/cms/articledetail'
import ArticleCategoryTree from '@/views/cms/articlecategory/ArticleCategoryTree.vue'
import { ArticleCategoryVO } from '@/api/cms/articlecategory'
import { ref } from 'vue'
import { DICT_TYPE } from '@/utils/dict'

/** 文章 列表 */
defineOptions({ name: 'ArticleDetailSimpleList' })

const loading = ref(true) // 列表的加载中
const list = ref<ArticleDetailVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  appSiteId: undefined as number | undefined,
  appId: undefined as string | undefined,
  categoryId: undefined as number | undefined,
  title: undefined,
  subTitle: undefined,
  status: undefined,
  viewNum: undefined,
  sort: undefined,
  createTime: []
})
const curArticleCategoryVO = ref<ArticleCategoryVO>()

/** 处理某一行的点击 */
const handleRowClick = (row: ArticleDetailVO) => {
  emits('row-click', row)
}
const emits = defineEmits(['row-click'])

const handleArticleCategoryTreeRowClick = (row: ArticleCategoryVO) => {
  queryParams.appSiteId = row.appSiteId
  queryParams.appId = row.appId
  queryParams.categoryId = row.id
  curArticleCategoryVO.value = row
  handleQuery()
  handleRowClick({
    appSiteId: row.appSiteId,
    appId: row.appId,
    categoryId: row.id
  } as ArticleDetailVO)
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ArticleDetailApi.getArticleDetailPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
